import Vue from 'vue'
import Router from 'vue-router'
import store from './store'
Vue.use(Router)



import Index from './views/Index.vue'
import Login from './views/Login.vue'
import Register from './views/Register.vue'
import NotFound from './views/errorPage/404.vue'

//主页
import dashboard from './views/dashboard/dashboard.vue'

let router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      component: Index,
      children: [
        {
          path: '/user/profile',
          name: 'profile',
          component: () => import('./views/company/user/profile/index'),
          meta:{title:'个人中心',authority:true}
        },
        {
          path: '/webClassify',
          name: 'webClassify',
          component: () => import('./views/WebClassify'),
          meta:{title:'前端字典数据查询',authority:true}
        },
        {
          path: '/dashboard',
          name: 'dashboard',
          component: dashboard,
          meta:{title:'首页',authority:true}
        },
        {
          path: '/storeHelp',
          name: 'storeHelp',
          component: () => import('./views/dashboard/storeHelp.vue'),
          meta:{title:'商家帮助',authority:true}
        },
        /*用户管理*/
        {
          path: '/user/userOrderDetail',
          name: 'userOrderDetail',
          component: () => import('./views/user/userOrderDetail'),
          meta:{title:'用户订单明细',authority:true}
        },
        {
          path: '/user/orderIntegralSet',
          name: 'orderIntegralSet',
          component: () => import('./views/user/orderIntegralSet'),
          meta:{title:'订单积分管理',authority:true}
        },
        {
          path: '/user/integralConversionAudit',
          name: 'integralConversionAudit',
          component: () => import('./views/user/integralConversionAudit'),
          meta:{title:'积分兑换审核',authority:true}
        },
        /*权限设置*/
        {
          path: '/jurisdiction/user',
          name: 'user',
          component: () => import('./views/jurisdiction/user/user'),
          meta:{title:'用户查询',authority:false}
        },
        {
          path: '/jurisdiction/role',
          name: 'role',
          component: () => import('./views/jurisdiction/role/role'),
          meta:{title:'角色管理',authority:false}
        },
        {
          path: '/jurisdiction/menu',
          name: 'menu',
          component: () => import('./views/jurisdiction/menu'),
          meta:{title:'菜单管理',authority:true}
        },
        {
          path: '/jurisdiction/roleAuth',
          name: 'roleAuth',
          component: () => import('./views/jurisdiction/role/roleAuth'),
          meta:{title:'分配角色',authority:false}
        },
        /*系统信息*/
        {
          path: '/system/label',
          name: 'label',
          component: () => import('./views/system/label'),
          meta:{title:'标签查询',authority:false}
        },
        {
          path: '/system/params',
          name: 'params',
          component: () => import('./views/system/params'),
          meta:{title:'参数设置',authority:false}
        },
        {
          path: '/system/activeLog',
          name: 'activeLog',
          component: () => import('./views/system/activeLog'),
          meta:{title:'操作日志',authority:false}
        },
        {
          path: '/system/loginLog',
          name: 'loginLog',
          component: () => import('./views/system/loginLog'),
          meta:{title:'登录设置',authority:false}
        },
        {
          path: '/system/task',
          name: 'task',
          component: () => import('./views/system/task'),
          meta:{title:'任务提醒',authority:false}
        },
        /*商品*/
        {
          path: '/shop/shopGroup',
          name: 'shopGroup',
          component: () => import('./views/shop/shopGroup'),
          meta:{title:'商品组',authority:true}
        },
        {
          path: '/shop/shopClassify',
          name: 'shopClassify',
          component: () => import('./views/shop/shopClassify'),
          meta:{title:'订单分类',authority:false}
        },
        {
          path: '/shop/shopIssue',
          name: 'shopIssue',
          component: () => import('./views/shop/shopIssue'),
          meta:{title:'好店商品',authority:false}
        },
        {
          path: '/shop/goodShop',
          name: 'goodShop',
          component: () => import('./views/shop/goodShop'),
          meta:{title:'好礼商品',authority:false}
        },
        {
          path: '/shop/shopInventory',
          name: 'shopInventory',
          component: () => import('./views/shop/shopInventory'),
          meta:{title:'商品库存',authority:false}
        },
        {
          path: '/shop/shopDetails',
          name: 'shopDetails',
          component: () => import('./views/shop/shopDetails'),
          meta:{title:'商品详情',authority:true}
        },
        {
          path: '/shop/editShop',
          name: 'editShop',
          component: () => import('./views/shop/shopDetails'),
          meta:{title:'修改商品',authority:true}
        },
        {
          path: '/shop/addShop',
          name: 'addShop',
          component: () => import('./views/shop/addShop'),
          meta:{title:'添加商品',authority:true}
        },
        {
          path: '/shop/applyGoodShop',
          name: 'applyGoodShop',
          component: () => import('./views/shop/applyGoodShop'),
          meta:{title:'申请好礼专区',authority:true}
        },

        /*订单*/
        {
          path: '/order/orderDetail',
          name: 'orderDetail',
          component: () => import('./views/order/orderDetail'),
          meta:{title:'订单详情',authority:true}
        },
        {
          path: '/order/shopOrder',
          name: 'shopOrder',
          component: () => import('./views/order/shopOrder'),
          meta:{title:'商品订单',authority:false}
        },
        {
          path: '/order/qRCode',
          name: 'qRCode',
          component: () => import('./views/order/qRCode'),
          meta:{title:'扫码订单',authority:false}
        },
        /*财务管理*/
        {
          path: '/finance/orderClose',
          name: 'orderClose',
          component: () => import('./views/finance/orderClose'),
          meta:{title:'订单结算',authority:false}
        },
        {
          path: '/finance/orderRefund',
          name: 'orderRefund',
          component: () => import('./views/finance/orderRefund'),
          meta:{title:'订单退款',authority:false}
        },
        {
          path: '/finance/upMoneyDetail',
          name: 'upMoneyDetail',
          component: () => import('./views/finance/upMoneyDetail'),
          meta:{title:'提额明细',authority:false}
        },
        /*门店信息*/
        {
          path: '/store/storeMsg',
          name: 'storeMsg',
          component: () => import('./views/store/storeMsg'),
          meta:{title:'门店信息',authority:false}
        },
        {
          path: '/store/goodStorePromotion',
          name: 'goodStorePromotion',
          component: () => import('./views/store/goodStorePromotion'),
          meta:{title:'好店推广位置',authority:false}
        },
        {
          path: '/store/storeAnnouncement',
          name: 'storeAnnouncement',
          component: () => import('./views/store/storeAnnouncement'),
          meta:{title:'门店公告',authority:false}
        },
        /*素材管理*/
        {
          path: '/material/material',
          name: 'material',
          component: () => import('./views/material/material'),
          meta:{title:'素材库',authority:false}
        },
        /*报表统计*/
        {
          path: '/statement/orderMarket',
          name: 'orderMarket',
          component: () => import('./views/statement/orderMarket'),
          meta:{title:'订单销售报表',authority:false}
        },
        {
          path: '/statement/shopMarket',
          name: 'shopMarket',
          component: () => import('./views/statement/shopMarket'),
          meta:{title:'商品销售报表',authority:false}
        },
        {
          path: '/statement/earnings',
          name: 'earnings',
          component: () => import('./views/statement/earnings'),
          meta:{title:'总收益统计',authority:false}
        },
				{
				  path: '/404',
				  name: '404',
          meta: {
              title: '404',
              authority:true
          },
				  component: NotFound
				},

      ]
    },
    {
      path: '/login',
    	name:'login',
      component: Login
    },
    {
      path: '/register',
    	name:'register',
    	component: Register
    },
    {
      path: '*',
      redirect: { path: '/404' }
    }
  ]
})
//console.log(store.state.menuList);

/*刷新页面权限设置*/
let menuStatusList = [];/*有权限的页面*/
let menuList = store.state.menuList.length?JSON.parse(store.state.menuList):[];
for(let i=0;i<menuList.length;i++){
  if(menuList[i].children){
    for(let a = 0;a<menuList[i].children.length;a++){
      menuStatusList.push(menuList[i].children[a])
        if(menuList[i].children[a].children){
           for(let b = 0;b<menuList[i].children[a].children.length;b++){
             menuStatusList.push(menuList[i].children[a].children[b])
           }
        }
    }
  }

}
for(let a=0;a<router.options.routes[0].children.length;a++){
  for(let i=0;i<menuStatusList.length;i++){
    if(router.options.routes[0].children[a].path == menuStatusList[i].component){
      router.options.routes[0].children[a].meta.authority = true;
    }
  }
}
/*刷新页面权限设置*/

// 全局路由守卫
router.beforeEach((to, from, next) => {
  let isAuthority = to.meta.authority;//是否有权限进入该页面
  const nextRoute = [ 'login', 'register'];//不需要登入权限的页面
	if (!(nextRoute.indexOf(to.name) >= 0)){  // 判断该路由是否需要登录权限
		const LOGINURL = '/login';
		let url = to.path;
		let user = localStorage.getItem('appToken');
		if (url === LOGINURL) {
			localStorage.removeItem('appToken')
		}
		// 判断是否登录
		if (!user && url !== LOGINURL) {
			next({ path: LOGINURL })
		}else if(url=='/' && user){
      next({path: '/dashboard'})
    }else if(!isAuthority){
      next({path: '/404'})
    }else {
			next()
		}
	}else{
		next()
	}
})

export default router
